//
//  LibraryViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/8.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "LibraryViewController.h"
#import "LibraryCollectionViewCell.h"
#import "LibraryCollectionViewLayout.h"
#import "PhotosManager.h"

@interface LibraryViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UIImageView      *bgImageV;
    UICollectionView *cardCollectionVIew;
    NSArray          *imageArray;
    
    CGFloat          startX;
    CGFloat          endX;
    NSInteger        currentIndex;
    
}
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray<PHAsset *> *imageArray;
@end

@implementation LibraryViewController

static NSString * const reuseIdentifier = @"LibraryCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.collectionView];
    [self loadImageData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LibraryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.imageAsset = self.imageArray[indexPath.item];
    return cell;
}

#pragma mark UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    startX = scrollView.contentOffset.x;
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    endX = scrollView.contentOffset.x;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self cellToCenter];
    });
}
-(void)cellToCenter{
    //最小滚动距离
    float  dragMinDistance = cardCollectionVIew.bounds.size.width/20.0f;
    if (startX - endX >= dragMinDistance) {
        currentIndex -= 1; //向右
    }else if (endX - startX >= dragMinDistance){
        currentIndex += 1 ;//向左
    }
    NSInteger maxIndex  = [cardCollectionVIew numberOfItemsInSection:0] - 1;
    currentIndex = currentIndex <= 0 ? 0 :currentIndex;
    currentIndex = currentIndex >= maxIndex ? maxIndex : currentIndex;
    
    bgImageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[currentIndex]]];
    
    [cardCollectionVIew scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    
}
- (void)loadImageData
{
    __weak typeof(self) weakSelf = self;
    
    [PhotosManager loadAllPhotosCompletionBlock:^(NSMutableArray<PHAsset *> *listArray) {
        weakSelf.imageArray = listArray;
        [weakSelf.collectionView reloadData];
    }];
}


#pragma mark - setter and getter

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        LibraryCollectionViewLayout *flatout = [[LibraryCollectionViewLayout alloc] init];
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:flatout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[LibraryCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        collectionView.alwaysBounceVertical = YES;
        collectionView.backgroundColor = [UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        imageView.image = [UIImage imageNamed:@"loginBack"];
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *effectVIew = [[UIVisualEffectView alloc]initWithEffect:effect];
        effectVIew.frame = imageView.bounds;
        [imageView addSubview:effectVIew];
        collectionView.backgroundView = imageView;

        
        _collectionView = collectionView;
    }
    return _collectionView;
}

@end

